/**************************************************************************
Step 1. Include necessary header files such that the stuff your test logic needs is declared. 
Don't forget gtest.h, which declares the testing framework.                                                   
**************************************************************************/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "gtest/gtest.h"
#include "include/pf_xml.h"


/**************************************************************************
Step 2. Use test fixture to design your complex cases. 
You can add SetUp() to initialize the test data and 
TearDown() to clear the test data for common usage
Pls remember that when you using test fixture, need to use macro "TEST_F" instead of "TEST"                                                 
**************************************************************************/
class PfXmlTest : public testing::Test 
{
    /* This SetUp() will be called before every TEST CASE which is marked by macro "TEST_F" */
    virtual void SetUp() 
    {
        xml_buffer_normal = 
            "<?xml version=\"1.0\"  encoding=\"ISO-8859-1\" standalone=\"no\"  ?> \n"
            "<!-- Edited with XML Spy v2007 (http://www.altova.com) --> \n"
            "<![CDATA[aabbccdd]]>"
            "<![CDATA[\n"
            "function matchwo(a,b)\n"
            "{\n"
            "if (a < b && a < 0) then\n"
            "  {\n"
            "  return 1;\n"
            "  }\n"
            "else\n"
            "  {\n"
            "  return 0;\n"
            "  }\n"
            "}\n"
            "]]>" 
            "<breakfast_menu>\n"
            "<food>\n"
              "<name nickname=\"mickey\" bigname=\"mouse\" aa=\"mouse\" bb = \"mouse\" cc = \"mouse\">Belgian Waffles</name> \n"
              "<price>$5.95</price> \n"
              "<description>two of our famous Belgian Waffles with plenty of real maple syrup</description> \n"
              "<calories>650</calories> \n"
              "</food>\n"
            "<food>\n"
              "<name>Strawberry Belgian Waffles</name> \n"
              "<price ischeap=\"yes\"  time=\"today\">$7.95</price> \n"
              "<description>light Belgian waffles covered with strawberries and whipped cream</description> \n"
              "<calories>900</calories> \n"
              "</food>\n"
            "<food>\n"
              "<name>Berry-Berry Belgian Waffles</name> \n"
              "<price>$8.95</price> \n"
              "<description tag=\"fresh berries\">light Belgian waffles covered with an assortment of fresh berries and whipped cream</description> \n"
              "<calories>900</calories> \n"
              "</food>\n"
            "<food>\n"
              "<name>French Toast</name> \n"
              "<price>$4.50</price> \n"
              "<description>thick slices made from our homemade sourdough bread</description> \n"
              "<calories same=\"800\">600</calories> \n"
              "</food>\n"
            "<food>\n"
              "<name>Homestyle Breakfast</name> \n"
              "<price>$6.95</price> \n"
              "<description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description> \n"
              "<calories>950</calories> \n"
             "</food>\n"
             "<![CDATA[aabbccdd]]>"
             " </breakfast_menu>\n";
        xml_buffer_exception1 = "<?xml version=\"1.0\" aa=\"bb\"?>\n";
        xml_buffer_exception2 = 
            "<?xml version=\"1.0\"?> \n"
            "<root></root1>";
        xml_buffer_exception3 = 
            "<?xml version=\"1.0\"?> \n"
            "<root>\n"
            "<food>aa</>"
            "</root>\n";
       xml_buffer_exception4 = 
            "<?xml version=\"1.0\"?> \n"
            "<root>\n"
            "<>aa</food>"
            "</root>\n";
        xml_buffer_exception5 = 
            "<?xml version=\"1.0\"?> \n"
            "<root>\n"
            "<food color=>aa</food>"
            "</root>\n";
        xml_buffer_exception6 = 
            "<?xml version=\"1.0\"?> \n"
            "<root>\n"
            "<food  taste = \"good\" color=red>aa</food>"
            "</root>\n";
        xml_buffer_exception7 = 
            "<?xml version=\"1.0\"?> \n"
            "<root>\n"
            "<food  taste = \"good\"a color=\"red\">aa</food>"
            "</root>\n";
        xml_buffer_exception8 = 
            "<?xml version=\"1.0\"?>\n"
            "<root>\n"
            " <food taste=\"good\" color=\"red\">\n"
            "  aa\n"
            " </food>\n"
            " <?aa kk=\"1.0\"?>\n"
            "</root>\n";
        xml_buffer_exception9 = 
            "<?xml version=\"1.0\"?>\n"
            "<root>\n"
            " <food taste=\"good\" color=\"red\">\n"
            "  aa\n"
            " </food>\n"
            " <?aa kk=\"1.0\"?>\n"
            " <!DOCTYPE aaaaaaaaabbb>\n"
            " <PRE> preprepree </PRE>\n"
            " <Script> javascript </Script>\n"
            " <!DOCTYPE aaaaaaaaabbb>\n"
            "</root>\n";
        xml_buffer_exception10 =
            "<?xml version=\"1.0\">\n"
            "<root>\n"
            "</root>\n";
        
        xml_buffer_exception11 =
            "<?xml version=\"1.0\"?>\n"
            "<root>\n"
            "<root>\n";
            
        xml_buffer_exception12 =
            "<?xml version=\"1.0\"?>\n"
            "<root>\n"
            "<to></to/>\n"
            "<from>John</from>\n"
            "</root>\n";
        xml_buffer_exception122 =
            "<?xml version=\"1.0\"?>\n"
            "<root>\n"
            "<to></to/a>\n"
            "<from>John</from>\n"
            "</root>\n";

        xml_buffer_exception123 =
            "<?xml version=\"1.0\"?>\n"
            "<root>\n"
            "<to></to/";

        xml_buffer_exception13 =
            "<?xml version=\"1.0\"?>\n"
            "<root>\n"
            "<to id=\"hello\'></to>\n"
            "<from>John</from>\n"
            "</root>\n";
        xml_buffer_exception14 =
            "<?xml version=\"1.0\"?>\n"
            "<root>\n"
            "<to id=\"hello></to>\n"
            "<from>John</from>\n"
            "</root>\n";



    }
    /* This TearDown will be called after every TEST CASE which is marked by macro "TEST_F" */
    virtual void TearDown() 
    {

    }

public:
    const char* xml_buffer_normal;
    const char* xml_buffer_exception1;
    const char* xml_buffer_exception2;
    const char* xml_buffer_exception3;
    const char* xml_buffer_exception4;
    const char* xml_buffer_exception5;
    const char* xml_buffer_exception6;
    const char* xml_buffer_exception7;

    const char* xml_buffer_exception8;
    const char* xml_buffer_exception9;
    const char* xml_buffer_exception10;
    const char* xml_buffer_exception11;
    const char* xml_buffer_exception12;
    const char* xml_buffer_exception122;
    const char* xml_buffer_exception123;
    const char* xml_buffer_exception13;
    const char* xml_buffer_exception14;

};

/***************************************************************************
Step 3. Use macro "TEST_F" to start your test case                                      
You can design your test case as the example
***************************************************************************/
TEST_F(PfXmlTest, parse_normal)
{
    pf_xml_t* xml = NULL;
    pf_xml_element_t* root = NULL;
    pf_xml_element_t* element = NULL;
    pf_xml_element_t* e_element = NULL;
    pf_xml_attribute_t* attr = NULL;
    char* name = NULL; 
    char* outstr = NULL;
    char* encoding = NULL;
    char* version = NULL;
    char* standalone = NULL;
    int idx = 0;
    int e_idx = 0;
    int a_idx  = 0;
    int ret = 0;

    xml = pf_xml_parse(xml_buffer_normal);
    EXPECT_NE(xml, (void*)NULL);
    root = pf_xml_get_root(xml);
    EXPECT_NE(root, (void*)NULL);
    
    name = pf_xml_elem_get_name(root);
    EXPECT_STREQ(name, "breakfast_menu");

    while((element = pf_xml_enum_elements(root, &idx)) != NULL)
    {
        EXPECT_STREQ(pf_xml_elem_get_name(element), "food");

        e_idx = 0;
        while((e_element = pf_xml_enum_elements(element, &e_idx)) != NULL)
            {
                //printf("idx = %d,name= %s \n",e_idx,pf_xml_elem_get_name(e_element));
                if(e_idx == 1)
                {
                   EXPECT_STREQ(pf_xml_elem_get_name(e_element), "name");
                  if(idx == 1) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "Belgian Waffles");
                  if(idx == 2) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "Strawberry Belgian Waffles");
                  if(idx == 3) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "Berry-Berry Belgian Waffles");
                  if(idx == 4) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "French Toast");
                  if(idx == 5) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "Homestyle Breakfast");
                   a_idx = 0;
                   while((attr = pf_xml_enum_attributes(e_element, &a_idx)) != NULL)
                   {
                       if(a_idx == 1)
                       {
                           EXPECT_STREQ(pf_xml_attrib_get_name(attr),"nickname");
                           EXPECT_STREQ(pf_xml_attrib_get_value(attr),"mickey");
                       }
                       if(a_idx == 2)
                       {
                           EXPECT_STREQ(pf_xml_attrib_get_name(attr),"bigname");
                           EXPECT_STREQ(pf_xml_attrib_get_value(attr),"mouse");
                       }
                       if(a_idx == 3)
                       {
                           EXPECT_STREQ(pf_xml_attrib_get_name(attr),"aa");
                           EXPECT_STREQ(pf_xml_attrib_get_value(attr),"mouse");
                       }
                       if(a_idx == 4)
                       {
                           EXPECT_STREQ(pf_xml_attrib_get_name(attr),"bb");
                           EXPECT_STREQ(pf_xml_attrib_get_value(attr),"mouse");
                       }
                       if(a_idx == 5)
                       {
                           EXPECT_STREQ(pf_xml_attrib_get_name(attr),"cc");
                           EXPECT_STREQ(pf_xml_attrib_get_value(attr),"mouse");
                       }
                   }
                }
                if(e_idx == 2)
                {
                   EXPECT_STREQ(pf_xml_elem_get_name(e_element), "price");
                   if(idx == 1) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "$5.95");
                   if(idx == 2) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "$7.95");
                   if(idx == 3) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "$8.95");
                   if(idx == 4) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "$4.50");
                   if(idx == 5) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "$6.95");
                   a_idx = 0;
                   while((attr = pf_xml_enum_attributes(e_element, &a_idx)) != NULL)
                   {
                       if(a_idx == 1)
                       {
                           EXPECT_STREQ(pf_xml_attrib_get_name(attr),"ischeap");
                           EXPECT_STREQ(pf_xml_attrib_get_value(attr),"yes");
                       }
                       if(a_idx == 2)
                       {
                           EXPECT_STREQ(pf_xml_attrib_get_name(attr),"time");
                           EXPECT_STREQ(pf_xml_attrib_get_value(attr),"today");
                       }
                   }
                }
                if(e_idx == 3)
                {
                   EXPECT_STREQ(pf_xml_elem_get_name(e_element), "description");
                   if(idx == 1) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "two of our famous Belgian Waffles with plenty of real maple syrup");
                   if(idx == 2) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "light Belgian waffles covered with strawberries and whipped cream");
                   if(idx == 3) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "light Belgian waffles covered with an assortment of fresh berries and whipped cream");
                   if(idx == 4) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "thick slices made from our homemade sourdough bread");
                   if(idx == 5) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "two eggs, bacon or sausage, toast, and our ever-popular hash browns");
                }
                if(e_idx == 4)
                {
                   EXPECT_STREQ(pf_xml_elem_get_name(e_element), "calories");
                   if(idx == 1) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "650");
                   if(idx == 2) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "900");
                   if(idx == 3) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "900");
                   if(idx == 4) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "600");
                   if(idx == 5) EXPECT_STREQ(pf_xml_elem_get_text(e_element), "950");
                   a_idx = 0;
                   while((attr = pf_xml_enum_attributes(e_element, &a_idx)) != NULL)
                   {
                       if(a_idx == 1)
                       {
                           EXPECT_STREQ(pf_xml_attrib_get_name(attr),"same");
                           EXPECT_STREQ(pf_xml_attrib_get_value(attr),"800");
                       }
                   }

                }

        }
    }
    EXPECT_EQ(idx, 6);

    element = pf_xml_find_element(root, "food");
    EXPECT_NE(element, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_name(element), "food");
    element = pf_xml_find_element(root, "food/");
    EXPECT_NE(element, (void*)NULL);

    element = pf_xml_find_element(element, "name");
    EXPECT_NE(element, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_text(element), "Belgian Waffles");
    attr = pf_xml_find_attribute(element, "bigname");
    EXPECT_STREQ(pf_xml_attrib_get_value(attr),"mouse");
    attr = pf_xml_find_attribute(element, "nickname");
    EXPECT_STREQ(pf_xml_attrib_get_value(attr),"mickey");

    element = pf_xml_find_element(root, "food/name");
    EXPECT_NE(element, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_text(element), "Belgian Waffles");

    version = pf_xml_get_version(xml);
    EXPECT_STREQ(version, "1.0");
    encoding = pf_xml_get_encoding(xml);
    EXPECT_STREQ(encoding, "ISO-8859-1");
    standalone = pf_xml_get_standalone(xml);
    EXPECT_STREQ(standalone, "no");

    outstr = pf_xml_dump(xml, 1);
    EXPECT_NE(outstr, (void*)NULL);
    EXPECT_GT(strlen(outstr), strlen(xml_buffer_normal));
    if(outstr)
    {
        //printf("dump xml:%s", outstr);
        free(outstr);
    }

    outstr = pf_xml_dump(xml, 0);
    EXPECT_NE(outstr, (void*)NULL);
    EXPECT_EQ(strlen(outstr), (size_t)1339);
    if(outstr)
    {
        //printf("dump xml:%s", outstr);
        free(outstr);
    }

    ret = pf_xml_clearup(xml);
    EXPECT_EQ(ret, 0);
    ret = pf_xml_destroy(xml);
    EXPECT_EQ(ret, 0);

} 
TEST_F(PfXmlTest, create_xml)
{
    pf_xml_t* xml = NULL;
    pf_xml_element_t* root = NULL;
    pf_xml_element_t* element = NULL;
    pf_xml_element_t* tmp_element = NULL;
    pf_xml_text_t* text = NULL;
    pf_xml_attribute_t* attr = NULL;
    pf_xml_clear_t* clear = NULL;
    char* version = NULL;
    char* encoding = NULL;
    char* standalone = NULL;
    char* outstr = NULL;
    //char* value = NULL;
    char * tar_str = 
        "<?xml version=\"1.1\" encoding=\"utf-8\"?>\n"
        "<root>\n"
        " <food>\n"
        "  <name nickname1=\"mickey1\" nickname2=\"mickey2\" nickname3=\"mickey3\" nickname4=\"mickey4\" nickname5=\"\" nickname6=\"mickey6\">\n"
        "   Belgian Waffles\n"
        "  </name>\n"
        "  <price>\n"
        "   $5.95\n"
        "  </price>\n"
        "  <description>\n"
        "   two of our famous Belgian Waffles with plenty of real maple syrup\n"
        "  </description>\n"
        "  <calories>\n"
        "   650\n"
        "  </calories>\n"
        "  <weight>\n"
        "   200g\n"
        "  </weight>\n"
        " </food>\n"
        " <food href=\"aa.html\"/>\n"
        " <food href=\"bb.html\"/>\n"
        " <food href=\"cc.html\"/>\n"
        " <foodaaa href=\"dd.html\"/>\n"
        " <![CDATA[cdata 's test: hello world]]>\n"
        "</root>\n";
    xml = pf_xml_create("root");
    EXPECT_NE(xml, (void*)NULL);

    pf_xml_init_declare(xml, "1.1", "utf-8", NULL);
    version = pf_xml_get_version(xml);
    EXPECT_STREQ(version, "1.1");
    encoding = pf_xml_get_encoding(xml);
    EXPECT_STREQ(encoding, "utf-8");
    standalone = pf_xml_get_standalone(xml);
    EXPECT_EQ(standalone, (void*)NULL);

    root = pf_xml_get_root(xml);
    EXPECT_NE(root, (void*)NULL);

    element = pf_xml_insert_element(root, "food"); 
    EXPECT_NE(element, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_name(element), "food");

    tmp_element = pf_xml_insert_element(element, "name");
    EXPECT_STREQ(pf_xml_elem_get_name(tmp_element), "name");
    text = pf_xml_insert_text(tmp_element, "Belgian Waffles");
    EXPECT_NE(text, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_text(tmp_element), "Belgian Waffles");
    text = pf_xml_insert_text(tmp_element, "Belgian Waffles a afafafa");
    EXPECT_EQ(text, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_text(tmp_element), "Belgian Waffles");
    attr = pf_xml_insert_attribute(tmp_element, "nickname1","mickey1");
    EXPECT_NE(attr, (void*)NULL);
    attr = pf_xml_insert_attribute(tmp_element, "nickname2","mickey2");
    EXPECT_NE(attr, (void*)NULL);
    attr = pf_xml_insert_attribute(tmp_element, "nickname3","mickey3");
    EXPECT_NE(attr, (void*)NULL);
    attr = pf_xml_insert_attribute(tmp_element, "nickname4","mickey4");
    EXPECT_NE(attr, (void*)NULL);
    attr = pf_xml_insert_attribute(tmp_element, "nickname5","");
    EXPECT_NE(attr, (void*)NULL);
    attr = pf_xml_insert_attribute(tmp_element, "nickname6","mickey6");
    EXPECT_NE(attr, (void*)NULL);

    attr = pf_xml_find_attribute(tmp_element, "nickname1");
    EXPECT_STREQ(pf_xml_attrib_get_name(attr), "nickname1");
    EXPECT_STREQ(pf_xml_attrib_get_value(attr), "mickey1");
    attr = pf_xml_find_attribute(tmp_element, "nickname3");
    EXPECT_STREQ(pf_xml_attrib_get_name(attr), "nickname3");
    EXPECT_STREQ(pf_xml_attrib_get_value(attr), "mickey3");
    attr = pf_xml_find_attribute(tmp_element, "nickname5");
    EXPECT_STREQ(pf_xml_attrib_get_name(attr), "nickname5");
    EXPECT_STREQ(pf_xml_attrib_get_value(attr), "");
    attr = pf_xml_find_attribute(tmp_element, "nickname6");
    EXPECT_STREQ(pf_xml_attrib_get_name(attr), "nickname6");
    EXPECT_STREQ(pf_xml_attrib_get_value(attr), "mickey6");


    tmp_element = pf_xml_insert_element(element, "price");
    EXPECT_STREQ(pf_xml_elem_get_name(tmp_element), "price");
    text = pf_xml_insert_text(tmp_element, "$5.95");
    EXPECT_NE(text, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_text(tmp_element), "$5.95");


    tmp_element = pf_xml_insert_element(element, "description");
    EXPECT_STREQ(pf_xml_elem_get_name(tmp_element), "description");
    text = pf_xml_insert_text(tmp_element, "two of our famous Belgian Waffles with plenty of real maple syrup");
    EXPECT_NE(text, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_text(tmp_element), "two of our famous Belgian Waffles with plenty of real maple syrup");


    tmp_element = pf_xml_insert_element(element, "calories");
    EXPECT_STREQ(pf_xml_elem_get_name(tmp_element), "calories");
    text = pf_xml_insert_text(tmp_element, "650");
    EXPECT_NE(text, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_text(tmp_element), "650");


    tmp_element = pf_xml_insert_element(element, "weight");
    EXPECT_STREQ(pf_xml_elem_get_name(tmp_element), "weight");
    text = pf_xml_insert_text(tmp_element, "200g");


    element = pf_xml_insert_element(root, "food"); 
    EXPECT_NE(element, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_name(element), "food");
    attr = pf_xml_insert_attribute(element, "href","aa.html");
    
    element = pf_xml_insert_element(root, "food"); 
    EXPECT_NE(element, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_name(element), "food");
    attr = pf_xml_insert_attribute(element, "href","bb.html");
    
    element = pf_xml_insert_element(root, "food"); 
    EXPECT_NE(element, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_name(element), "food");
    attr = pf_xml_insert_attribute(element, "href","cc.html");
    
    element = pf_xml_insert_element(root, "foodaaa"); 
    EXPECT_NE(element, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_name(element), "foodaaa");
    attr = pf_xml_insert_attribute(element, "href","dd.html");
    
    clear = pf_xml_insert_cdata(root, "cdata 's test: hello world");


    outstr = pf_xml_dump(xml, 1);
    EXPECT_NE(outstr, (void*)NULL);
    EXPECT_EQ(strlen(outstr), strlen(tar_str));
    EXPECT_STREQ(outstr, tar_str);
    if(outstr)
    {
        free(outstr);
    }
    pf_xml_destroy(xml);
} 
TEST_F(PfXmlTest, parse_xml_exception)
{
    pf_xml_t* xml = NULL;
    pf_xml_element_t* root = NULL;
    char* outstr = NULL;

    xml = pf_xml_parse(xml_buffer_exception1);
    EXPECT_NE(xml, (void*)NULL);
    root = pf_xml_get_root(xml);
    EXPECT_EQ(root, (void*)NULL);
    EXPECT_STREQ(pf_xml_get_version(xml), "1.0");
    EXPECT_STREQ(pf_xml_get_encoding(xml), NULL);
    EXPECT_STREQ(pf_xml_get_standalone(xml), NULL);

    outstr = pf_xml_dump(xml, 0);
    EXPECT_NE(outstr, (void*)NULL);
    if(outstr)
    {
        free(outstr);
    }
    pf_xml_clearup(xml);
    pf_xml_destroy(xml);

    xml = pf_xml_parse(xml_buffer_exception2);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer_exception3);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer_exception4);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer_exception5);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer_exception6);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer_exception7);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer_exception8);
    EXPECT_NE(xml, (void*)NULL);
    outstr = pf_xml_dump(xml, 1);
    EXPECT_NE(outstr, (void*)NULL);
    EXPECT_STREQ(outstr, xml_buffer_exception8);
    if(outstr)
    {
        free(outstr);
    }
    pf_xml_destroy(xml);

    xml = pf_xml_parse(xml_buffer_exception9);
    EXPECT_NE(xml, (void*)NULL);
    outstr = pf_xml_dump(xml, 1);
    EXPECT_NE(outstr, (void*)NULL);
    EXPECT_STREQ(outstr, xml_buffer_exception9);
    if(outstr)
    {
        //printf("dump: %s \n", outstr);
        free(outstr);
    }
    pf_xml_destroy(xml);

    xml = pf_xml_parse(xml_buffer_exception10);
    EXPECT_EQ(xml, (void*)NULL);
   
    xml = pf_xml_parse(xml_buffer_exception11);
    EXPECT_NE(xml, (void*)NULL);
    pf_xml_destroy(xml);
    
    xml = pf_xml_parse(xml_buffer_exception12);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer_exception122);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer_exception123);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer_exception13);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer_exception14);
    EXPECT_EQ(xml, (void*)NULL);



}
TEST_F(PfXmlTest, parse_xml_extra)
{
    pf_xml_t* xml = NULL;
    pf_xml_element_t* element = NULL;
    pf_xml_element_t* root = NULL;
    char* outstr = NULL;
    char* xml_buffer = 
        "<root1>aa</root1>\n"
        "<root2>bb</root2>\n";
    char* xml_buffer1 = 
        "<?xml aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbo"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbo"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbo"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbo"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbo"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbo"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbo"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbo"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbo"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbo"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbo"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbo?>"
        "<root1>aa</root1>\n"
        "<root2>bb</root2>\n";
   
    char* xml_buffer2 = 
        "<root>"
        "<root1>aa=bb<root1>\n"
        "<root2>bb=cc</root2>\n"
        "</root>";

    char* xml_buffer3 = 
        "<root>"
        "<root1 <?ddd?>=\"123\">aa=bb</root1>\n"
        "<root2>bb=cc</root2>\n"
        "</root>";

    char* xml_buffer4 = 
        "<root>"
        "<root1 ddd=\"123\">>aa=bb</root1>\n"
        "<root2>bb=cc</root2>\n"
        "</root>";

    char* xml_buffer5 = 
        "<root>"
        "<root1>aabb<?aa?></root1>\n"
        "<root2>bb=cc</root2>\n"
        "</root>";

    char* xml_buffer6 = 
        "<root>"
        "<root1 aa/>></root1>\n"
        "<root2>bb=cc</root2>\n"
        "</root>";

    char* xml_buffer7 = 
        "<root>"
        "<root1 aa=\"bb\"/>\n"
        "<root2>bb=cc</root2>\n"
        "</root>";


    xml = pf_xml_create("root");
    EXPECT_NE(xml, (void*)NULL);

    root = pf_xml_get_root(xml);
    EXPECT_NE(root, (void*)NULL);
    element = pf_xml_insert_element(root, "aaaabbbb");
    EXPECT_NE(element, (void*)NULL);

    outstr = pf_xml_dump(xml, 0); 
    EXPECT_NE(outstr, (void*)NULL);
    EXPECT_STREQ(outstr, "<root><aaaabbbb/></root>");
    if(outstr)
    {
        //printf("dump: %s \n", outstr);
        free(outstr);
    }
    pf_xml_destroy(xml);

    xml = pf_xml_parse(xml_buffer);
    EXPECT_NE(xml, (void*)NULL);
    root = pf_xml_get_root(xml);
    EXPECT_NE(root, (void*)NULL);
    EXPECT_STREQ(pf_xml_elem_get_name(root), "");
    pf_xml_destroy(xml);

    xml = pf_xml_parse(xml_buffer1);
    EXPECT_EQ(xml, (void*)NULL);
   
    xml = pf_xml_parse(xml_buffer2);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer3);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer4);
    EXPECT_EQ(xml, (void*)NULL);
    
    xml = pf_xml_parse(xml_buffer5);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer6);
    EXPECT_EQ(xml, (void*)NULL);

    xml = pf_xml_parse(xml_buffer7);
    EXPECT_NE(xml, (void*)NULL);
    pf_xml_destroy(xml);





}

